﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKBatchPrinter.Layout;

namespace YArchitech.Public.ExcelToView
{
	public partial class PreviewData : EscBaseForm
	{
		internal List<bool> Lstblresult
		{
			get
			{
				return this.blresult;
			}
		}

		public PreviewData(List<List<string>> excelData, ExternalCommandData commandData)
		{
			this.m_Doc = commandData.Application.ActiveUIDocument.Document;
			this.InitializeComponent();
			this.m_CommandData = commandData;
			this.GetExcelData(excelData);
			this.m_Manage = new LayoutManager(this.m_CommandData);
			this.m_Manage.LoadFamily();
		}

		public void GetExcelData(List<List<string>> excelData)
		{
			this.NewexcelData = excelData;
			int num = 0;
			string[] array = new string[4];
			if (excelData.Count == 0)
			{
				return;
			}
			this.dataGridView1.RowCount = excelData.Count;
			foreach (List<string> list in excelData)
			{
				array = list.ToArray();
				for (int i = 0; i < list.Count; i++)
				{
					this.dataGridView1.Rows[num].Cells[i].Value = array[i];
				}
				num++;
			}
		}

		private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		private List<ElementId> GetViewPortIDs(ViewSheet vs)
		{
			List<ElementId> list = new List<ElementId>();
			ICollection<ElementId> allViewports = vs.GetAllViewports();
			foreach (ElementId elementId in allViewports)
			{
				Viewport viewport = this.m_Doc.GetElement(elementId) as Autodesk.Revit.DB.Viewport;
				list.Add(viewport.ViewId);
			}
			return list;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			string[] exceldata = new string[4];
			ViewMaker viewMaker = new ViewMaker(this.m_Doc);
			string message = null;
			foreach (List<string> list in this.NewexcelData)
			{
				exceldata = list.ToArray();
				this.addedViewSheet = viewMaker.makeAViewSheet(list, out message);
				if (this.addedViewSheet == null)
				{
					YJKMessageBox.Show(message);
				}
				else
				{
					this.listViewSheets.Add(this.addedViewSheet);
					List<ElementId> viewPortIDs = this.GetViewPortIDs(this.addedViewSheet);
					DrawingFrameData data = this.GetData(exceldata);
					this.m_creatorViewSheet = new ViewSheetCreator(this.m_CommandData, data, this.addedViewSheet);
					if (!this.m_creatorViewSheet.CreateViewFrame())
					{
						MessageBox.Show("创建图框失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						this.blresult.Add(false);
						return;
					}
					this.blresult.Add(true);
					List<FrameData> list2 = new List<FrameData>();
					list2.Add(new FrameData
					{
						idViewSheet = this.m_creatorViewSheet.m_viewSheet.Id,
						listIdViewPort = viewPortIDs,
						type = FrameType.FT_OWNER
					});
					this.m_creatorViewSheet = new ViewSheetCreator(this.m_CommandData, list2);
					if (!this.m_creatorViewSheet.LayoutViewPort())
					{
						MessageBox.Show(this.m_creatorViewSheet.m_strPromptMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
						return;
					}
					base.DialogResult = DialogResult.OK;
					this.m_Manage.AddSharedPara();
				}
			}
			base.Close();
		}

		protected DrawingFrameData GetData(string[] exceldata)
		{
			DrawingFrameData result = default(DrawingFrameData);
			if (exceldata[3].Substring(0, 1) == "横")
			{
				result.sDrawingStyle = "横式";
			}
			else if (exceldata[3].Substring(0, 1) == "竖" || exceldata[3].Substring(0, 1) == "立")
			{
				result.sDrawingStyle = "立式";
			}
			else
			{
				result.sDrawingStyle = "横式";
			}
			result.sDrawingName = exceldata[1];
			result.sDrawingNum = exceldata[0];
			if (exceldata[2] != "A0" && exceldata[2] != "A1" && exceldata[2] != "A2" && exceldata[2] != "A3")
			{
				result.sDrawingType = exceldata[2];
				char[] array = new char[exceldata[2].Length];
				array = exceldata[2].ToCharArray();
				if (array[0] == 'A')
				{
					result.sDrawingType = exceldata[2].Substring(0, 2);
					if (result.sDrawingType == "A0")
					{
						if (exceldata[2].Substring(3) == "1")
						{
							result.dDrawingLength = 2378.0;
						}
						if (exceldata[2].Substring(3) == "1/4")
						{
							result.dDrawingLength = 1486.0;
						}
						if (exceldata[2].Substring(3) == "1/2")
						{
							result.dDrawingLength = 1783.0;
						}
						if (exceldata[2].Substring(3) == "3/4")
						{
							result.dDrawingLength = 2080.0;
						}
						if (exceldata[2].Substring(3) == "5/4")
						{
							result.dDrawingLength = 2675.0;
						}
						result.sDrawingSizeName = "A0+" + exceldata[2].Substring(3);
						result.dDrawingWidth = 841.0;
					}
					else if (result.sDrawingType == "A1")
					{
						if (exceldata[2].Substring(3) == "1")
						{
							result.dDrawingLength = 1682.0;
						}
						if (exceldata[2].Substring(3) == "1/4")
						{
							result.dDrawingLength = 1051.0;
						}
						if (exceldata[2].Substring(3) == "1/2")
						{
							result.dDrawingLength = 1261.0;
						}
						if (exceldata[2].Substring(3) == "3/4")
						{
							result.dDrawingLength = 1471.0;
						}
						if (exceldata[2].Substring(3) == "5/4")
						{
							result.dDrawingLength = 1892.0;
						}
						result.sDrawingSizeName = "A1+" + exceldata[2].Substring(3);
						result.dDrawingWidth = 594.0;
					}
					else if (result.sDrawingType == "A2")
					{
						if (exceldata[2].Substring(3) == "1")
						{
							result.dDrawingLength = 1189.0;
						}
						if (exceldata[2].Substring(3) == "1/4")
						{
							result.dDrawingLength = 743.0;
						}
						if (exceldata[2].Substring(3) == "1/2")
						{
							result.dDrawingLength = 891.0;
						}
						if (exceldata[2].Substring(3) == "3/4")
						{
							result.dDrawingLength = 1041.0;
						}
						if (exceldata[2].Substring(3) == "5/4")
						{
							result.dDrawingLength = 1338.0;
						}
						result.sDrawingSizeName = "A2+" + exceldata[2].Substring(3);
						result.dDrawingWidth = 420.0;
					}
					else if (result.sDrawingType == "A3")
					{
						if (exceldata[2].Substring(3) == "1")
						{
							result.dDrawingLength = 841.0;
						}
						if (exceldata[2].Substring(3) == "1/4")
						{
							result.dDrawingLength = 525.0;
						}
						if (exceldata[2].Substring(3) == "1/2")
						{
							result.dDrawingLength = 630.0;
						}
						if (exceldata[2].Substring(3) == "3/4")
						{
							result.dDrawingLength = 735.0;
						}
						if (exceldata[2].Substring(3) == "5/4")
						{
							result.dDrawingLength = 945.0;
						}
						result.sDrawingSizeName = "A3+" + exceldata[2].Substring(3);
						result.dDrawingWidth = 297.0;
					}
				}
				else
				{
					result.sDrawingType = "A1";
					result.sDrawingSizeName = "A1";
					result.dDrawingLength = 841.0;
					result.dDrawingWidth = 594.0;
				}
			}
			else if (exceldata[2] == "A0")
			{
				result.sDrawingType = "A0";
				result.sDrawingSizeName = "A0";
				result.dDrawingLength = 1189.0;
				result.dDrawingWidth = 841.0;
			}
			else if (exceldata[2] == "A1")
			{
				result.sDrawingType = "A1";
				result.sDrawingSizeName = "A1";
				result.dDrawingLength = 841.0;
				result.dDrawingWidth = 594.0;
			}
			else if (exceldata[2] == "A2")
			{
				result.sDrawingType = "A2";
				result.sDrawingSizeName = "A2";
				result.dDrawingLength = 594.0;
				result.dDrawingWidth = 420.0;
			}
			else if (exceldata[2] == "A3")
			{
				result.sDrawingType = "A3";
				result.sDrawingSizeName = "A3";
				result.dDrawingLength = 420.0;
				result.dDrawingWidth = 297.0;
			}
			result.sDrawSignBarName = this.m_sDrawSignBarName;
			result.sTitleBarName = this.m_sTitleBarName;
			return result;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void PreviewData_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private ViewSheetCreator m_creatorViewSheet;

		private string m_sDrawSignBarName = "HyCounterSign";

		private string m_sTitleBarName = "HyTitleBar";

		public ViewSheet m_viewSheet;

		public List<ViewSheet> listViewSheets = new List<ViewSheet>();

		private LayoutManager m_Manage;

		private Document m_Doc;

		private ViewSheet addedViewSheet;

		private List<List<string>> NewexcelData;

		private List<bool> blresult = new List<bool>();

		private ExternalCommandData m_CommandData;
	}
}
